<script lang="ts" setup>
import {messageStore} from "../store";
import {onUpdated, ref} from "vue";

onUpdated(() => console.log('hello'))

ref<string>('src/assets/img/01.jpg');
const store = messageStore()
const userList = store.getUserList();

let idx = ref<number>(-1)
const selectUser = (index: number) => {
  idx.value = index
}

const chooseUser = (id: number) => {
  store.setCurrentUserId(id)
  const box = document.querySelector('.messageWindow') as HTMLElement
  setTimeout(() => box.scrollTop = box.scrollHeight)
}

</script>
<template>
<div id="content">
  <div class="search">
    <div class="logo">Mini-Chat</div>
    <div class="input">
      <input type="text">
      <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="search bi bi-search" viewBox="0 0 16 16">
        <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
      </svg>
    </div>
  </div>
  <ul>
    <li v-for="(item, index) in userList" @click="selectUser(index); chooseUser(item.id)" :class="{selected: index === idx}">
      <div class="avatar" :style="{backgroundImage: 'url(' + item.avatar + ')'}"></div>
      <div>
        <h2 class="nickName">{{item.name}} </h2>
      </div>
    </li>
  </ul>
</div>
</template>
<style en="less" scoped>
.selected {
  background: linear-gradient(to right, #fa3b56, rgba(255, 255, 255, 0));
}
#content {
  height: 100%;
  width: 250px;
  background-color: var(--use-list-bg);
  border-right: var(--border) solid 2px;
  .search {
    width: 100%;
    height: 80px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    border-bottom: var(--border) solid 2px;
    .logo {
      width: 100%;
      text-align: center;
      height: 40px;
      color: var(--color-font);
      line-height: 40px;
      font-size: 20px;
    }
    .input {
      height: 40px;
      width: 100%;
      input {
        height: 20px;
        border: #2d2d38 solid 1px;
        float: left;
        border-radius: 8px;
        width: calc(100% - 60px);
        margin-top: 8px;
        margin-left: 15px;
        outline: none;
      }
      .search {
        margin-top: 8px;
        margin-right: 12px;
        border-radius: 5px;
        height: 20px;
        width: 20px;
        float: right;
        border: none;
        color: var(--color-font);
        &:active {
          color: #fa3b56;
        }
      }
    }
  }

  ul {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    height: 100%;
    width: 100%;
    overflow-y: scroll;
    &::-webkit-scrollbar {
      width: 4px;
      background-color: rgba(0, 0, 0, 0);
    }
    li {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      height: 70px;
      width: 100%;
      border-bottom: var(--border) solid 2px;
      &:hover {
        background-color: var(--hover-bg);
      }
      .avatar {
        height: 50px;
        width: 50px;
        margin-left: 20px;
        border-radius: 50%;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
      }
      div {
        width: calc(100% - 70px);
        display: flex;
        flex-direction: row;
        justify-content: center;
        .nickName {
          margin-right: 20px;
        }
      }

    }
  }
}
</style>